package com.awspaas.user.apps.cpq.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;

public class CPQ_GetTemplateSystemMsgWeb {

	public String getTemplateSystemMsg(UserContext userContext, String bindId, String xmlx, String cpxh,String dzsm) {
		try {
			List<BO> bos=SDK.getBOAPI().query("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_P").bindId(bindId).list();
			String sql="select a.XTMC,a.PARMENTIER_NAME,a.PARMENTIER_NO,a.PARMENTIER_VALUE,a.DW,a.BZ "
					+ "from BO_EU_CPQ_TEMPLATE_PARAMETER a LEFT JOIN BO_EU_CPQ_TEMPLATE_PRICE b on a.BINDID=b.BINDID " + 
					"  where  b.CPXH=? and b.XMLX=? and b.DZSM=?";
			List<RowMap> maps=DBSql.getMaps(sql, new Object[] {cpxh,xmlx,dzsm});
			Map<String, BO> tMap=new HashMap<String, BO>();
		    Map<String, RowMap> sMap=new HashMap<String, RowMap>();
			if(maps.size()>0) {
              for (RowMap rowMap : maps) {
	                   sMap.put(rowMap.getString("PARMENTIER_NO"), rowMap);;
                     }
			}else {
				ResponseObject ro = ResponseObject.newOkResponse();
				Map<String, String> map=new HashMap<String, String>();
				map.put("type", "0");
				ro.setData(map);
				 return ro.toString();
				
			}
			if(bos.size()>0){
				for (BO bo : bos) {
					String num= bo.getString("PARMENTIER_NO");					
					if(sMap.containsKey(num)) {
						RowMap data=sMap.get(num);
						bo.set("PARMENTIER_VALUE", data.getString("PARMENTIER_VALUE"));
						SDK.getBOAPI().update("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_P", bo);
					}
					tMap.put(num, bo);
				}
			}
			//将没有记录的参数信息，创建到安装费模板参数明细表中
			List<BO> lists=new ArrayList<BO>();
			for (String  key : sMap.keySet()) {
				if(!tMap.containsKey(key)) {
					RowMap row=sMap.get(key);
					BO pBo=new BO();
					pBo.set("XTMC", row.getString("XTMC"));
					pBo.set("PARMENTIER_NAME", row.getString("PARMENTIER_NAME"));
					pBo.set("PARMENTIER_NO", row.getString("PARMENTIER_NO"));
					pBo.set("PARMENTIER_VALUE", row.getString("PARMENTIER_VALUE"));
					pBo.set("DW", row.getString("DW"));
					pBo.set("BZ", row.getString("BZ"));
					lists.add(pBo);
				}
			}
			if(lists.size()>0) {
				SDK.getBOAPI().create("BO_EU_CQP_INSTALLATION_FEE_TEMPLATE_P", lists, bindId, userContext.getUID());
			}
			 ResponseObject ro = ResponseObject.newOkResponse();
			 Map<String, String> map=new HashMap<String, String>();
				map.put("type", "1");
				ro.setData(map);
			 return ro.toString();
		} catch (Exception e) {
			throw  e;
		}
		
	}

}
